package com.chaoyong.higo.view;



import com.chaoyong.higo.R;
import com.chaoyong.higo.base.StringAdapter;
import com.chaoyong.higo.base.StringCenterAdapter;
import com.chaoyong.higo.utils.RotateUtil;
import com.chaoyong.higo.utils.StringUtils;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

/**
 * 点击选择listview
 * 
 * @author ln
 */
public class SelectListView extends RelativeLayout {
	private ListView mListview;
	private ListView mListviewLeft;
	private ListView mListviewRight;
	private LinearLayout mLin;
	private StringAdapter mAdapter;
	private StringCenterAdapter mCenterAdapter;
	private StringCenterAdapter mCenterRightAdapter;
	private String[] mList;
	private ImageView mView;
	private OnSelectListener onSelectListener;
	private boolean isVisible;
	public String mListviewLeftName = "deal";

	public SelectListView(Context context) {
		super(context);
	}

	public SelectListView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	private void initView() {
		if (mListview == null) {
			LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			inflater.inflate(R.layout.select_listview, this);
			mView = (ImageView) findViewById(R.id.select_view);
			mListview = (ListView) findViewById(R.id.select_lv);
			mListviewLeft = (ListView) findViewById(R.id.select_lv_left);
			mListviewRight = (ListView) findViewById(R.id.select_lv_right);
			mLin = (LinearLayout) findViewById(R.id.select_ll_right);
			mView.setOnTouchListener(new OnTouchListener() {

				@Override
				public boolean onTouch(View v, MotionEvent event) {
					switch (event.getAction()) {
					case MotionEvent.ACTION_DOWN:
						if (isVisible)
							hideView();
						break;
					default:
						break;
					}
					return true;
				}
			});
		}
	}

	/**
	 * 干啥都运行他
	 * 
	 * @param list
	 */
	public void showView(final String[] list) {
		if (list == null) {
			hideView();
			return;
		}
		if (mAdapter == null) {
			initView();
			mList = list;
			mAdapter = new StringAdapter(getContext(), mList);
			mListview.setAdapter(mAdapter);
			mListview.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					if (onSelectListener != null)
						onSelectListener.onClick(mList[position], position);
					hideTypeLayout();
				}
			});
		}

		if (mLin.getVisibility() == View.GONE) {
			if (mList == null || StringUtils.arrayEquals(list, mList)) {
				this.mList = list;
				mAdapter.setList(list);
				showTypeLayout(mListview);
			} else {
				TranslateAnimation animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0,
						Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF,
						-1.0f);
				animation.setDuration(200);
				animation.setAnimationListener(new Animation.AnimationListener() {
					@Override
					public void onAnimationStart(Animation animation) {
					}

					@Override
					public void onAnimationRepeat(Animation animation) {
					}

					@Override
					public void onAnimationEnd(Animation animation) {
						mList = list;
						mAdapter.setList(list);
						TranslateAnimation a = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0,
								Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, -1.0f,
								Animation.RELATIVE_TO_SELF, 0);
						animation.setDuration(200);
						mListview.startAnimation(a);
					}
				});
				mListview.startAnimation(animation);
			}
		} else {
			TranslateAnimation animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0,
					Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, -1.0f);
			animation.setDuration(200);
			animation.setAnimationListener(new Animation.AnimationListener() {
				@Override
				public void onAnimationStart(Animation animation) {
				}

				@Override
				public void onAnimationRepeat(Animation animation) {
				}

				@Override
				public void onAnimationEnd(Animation animation) {
					mList = list;
					mAdapter.setList(list);
					TranslateAnimation a = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0,
							Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, -1.0f,
							Animation.RELATIVE_TO_SELF, 0);
					animation.setDuration(200);
					mListview.setVisibility(View.VISIBLE);
					mLin.setVisibility(View.GONE);
					mListview.startAnimation(a);
				}
			});
			mLin.startAnimation(animation);
		}
	}

	/**
	 * 两个的
	 * 
	 * @param list
	 */
	public void showView(final String[] listLeft, final String[] listRight) {
		if (mCenterAdapter == null) {
			initView();
			mCenterAdapter = new StringCenterAdapter(getContext(), listLeft);
			mCenterRightAdapter = new StringCenterAdapter(getContext(), listRight);
			mListviewLeft.setAdapter(mCenterAdapter);
			mListviewRight.setAdapter(mCenterRightAdapter);
			mListviewLeft.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					if (onSelectListener != null) {
						mCenterAdapter.setClick(position);
						switch (position) {
						case 0:
							mListviewLeftName = "deal";
							break;
						case 1:
							mListviewLeftName = "countnum";
							break;
						case 2:
							mListviewLeftName = "level";
							break;
						}
						// onSelectListener.onClick(listLeft[position],
						// position);
					}
				}
			});
			mListviewRight.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					if (onSelectListener != null) {
						onSelectListener.onClick(listRight[position], position);
					}
					hideTypeLayout();
				}
			});
		}
		if (mLin.getVisibility() == View.GONE) {
			mCenterAdapter.setList(listLeft);
			mCenterAdapter.setClick(0);
			mCenterRightAdapter.setList(listRight);
			if (mListview.getVisibility() == View.VISIBLE) {
				TranslateAnimation animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0,
						Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF,
						-1.0f);
				animation.setDuration(200);
				animation.setAnimationListener(new Animation.AnimationListener() {
					@Override
					public void onAnimationStart(Animation animation) {

					}

					@Override
					public void onAnimationRepeat(Animation animation) {

					}

					@Override
					public void onAnimationEnd(Animation animation) {
						TranslateAnimation a = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0,
								Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, -1.0f,
								Animation.RELATIVE_TO_SELF, 0);
						animation.setDuration(200);
						mListview.setVisibility(View.GONE);
						mLin.setVisibility(View.VISIBLE);
						mLin.startAnimation(a);
					}
				});
				mListview.startAnimation(animation);
			} else {
				showTypeLayout(mLin);
			}
		} else {
			hideView();
		}
	}

	/**
	 * 隐藏布局
	 */
	public void hideView() {
		if (mListview != null && mListview.getVisibility() == View.VISIBLE
				|| mLin != null && mLin.getVisibility() == View.VISIBLE)
			hideTypeLayout();
	}

	/**
	 * 显示 type 布局
	 */
	private void showTypeLayout(View view) {
		if (view.getVisibility() == View.GONE) {
			isVisible = true;
			view.setVisibility(View.VISIBLE);
			mView.setVisibility(View.VISIBLE);
			TranslateAnimation animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0,
					Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0);
			animation.setDuration(300);
			view.startAnimation(animation);
		} else {
			hideTypeLayout();
		}
	}

	private void hideTypeLayout() {
		isVisible = false;
		TranslateAnimation animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF,
				0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, -1.0f);
		animation.setDuration(300);
		animation.setAnimationListener(new Animation.AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				mListview.setVisibility(View.GONE);
				mView.setVisibility(View.GONE);
				mLin.setVisibility(View.GONE);
				mList = null;
			}
		});
		if (mListview.getVisibility() == View.VISIBLE)
			mListview.startAnimation(animation);
		else
			mLin.startAnimation(animation);
		RotateUtil.getInitRotate().cancelRotate();
	}

	public void setOnSelectListener(OnSelectListener selectListener) {
		if (onSelectListener == null) {
			onSelectListener = selectListener;
		}
	}

	public interface OnSelectListener {
		public void onClick(String name, int position);
	}

}
